package enemies
{
	import flash.display.MovieClip;
	import flash.geom.Point;
	
	public class Enemy extends MovieClip
	{
		var game_handler:GameHandler;
		var hp = 5;
		public var virtual_hp =5;
		public var died = false;
		public function Enemy(gH:GameHandler)
		{
			super();
			game_handler = gH;
			game_handler.enemy_units.push(this);
		}
		public function resize_me(){
			var resize_to = (this.y - game_handler.STAGE_HEIGHT/2)/game_handler.STAGE_HEIGHT/2*game_handler.resize_effect;
			this.scaleX = 1 + resize_to;
			this.scaleY = 1 + resize_to;
		}
		public function get_direction_of_castle(){
			var y_diff = game_handler.castle_center.y - this.y;
			var x_diff = game_handler.castle_center.x - this.x;
			var y_diff_sq = y_diff*y_diff;
			var x_diff_sq = x_diff*x_diff;
			
			if(x_diff_sq + y_diff_sq == 0)
				return new Point(0,0);
			
			return new Point( x_diff / Math.abs(x_diff) * (x_diff_sq/(x_diff_sq+y_diff_sq)),y_diff / Math.abs(y_diff) * (y_diff_sq/(x_diff_sq+y_diff_sq)));
		}
		public function get_distance_to_castle(){
			var y_diff = game_handler.castle_center.y - this.y;
			var x_diff = game_handler.castle_center.x - this.x;
			var y_diff_sq = y_diff*y_diff;
			var x_diff_sq = x_diff*x_diff;
			return Math.sqrt(y_diff_sq + x_diff_sq);
		}
		public function hit(type,amt){
			hp -= amt;
			if(hp <= 0)
				die();
		}
		public function virtual_hit(type,amt){
			virtual_hp -= amt;
		}
		public function die(){
			if(died)
				return;
			died = true;
			var my_spot=-1;
			for(var i:int = 0; i < game_handler.enemy_units.length; i++)
				if(game_handler.enemy_units[i] == this)
						my_spot = i;
			if(my_spot != -1)
				game_handler.enemy_units.splice(my_spot,1);
			game_handler.removeChild(this);
		}
	}
}